import { observable, action, autorun, toJS } from 'mobx';
import { WeaLocaleProvider } from 'ecCom';
import {WeaForm, WeaTableNew} from 'comsMobx'
import {message, Button } from "antd";
import * as Apis from '../../apis/info';
import {Text} from '../../components/util/text';
const text = new Text();

const {TableStore} = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;
export class specialDialogStore {

    @observable form = new WeaForm();      //特殊采用和专报form
    @observable condition = [];     //特殊采用和专报condition
    title = "";
    type = "";      //判断当前操作是特殊采用还是专报
    ids = "";
    by = "";        //判断请求来自于列表还是卡片

    parent = null;
    constructor(parent) {
        this.parent = parent;
    }

    @observable status = {
        visible: false     //特殊采用和专报dialog的visible
    }

    setState = (params = {}) => {
        Object.keys(params).forEach(o => {
            this.status[o] = params[o];
        })
        return this;
    }

    //初始化特殊采用，专报弹出框condition
    @action
    getSpecialHandleCondition = (params = {}) => {
        let newParams = {...params };
        this.type = params.type;
        this.by = params.by;
        this.ids = params.ids;
        this.form = new WeaForm();
        this.condition = [];
        Apis.getSpecialHandleCondition(newParams).then(data => {
            if (data.api_status == '1') {
                if (!data.isRight) {
                    message.error(getLabel(2012, '对不起，您暂时没有权限！'));
                    return;
                }
                this.setState({ visible: true })
                this.title = data.title;
                this.condition = data.infoConditionGroup;
                this.form && !this.form.isFormInit && this.form.initFormFields(this.condition);
            } else {
                message.error(data.api_errormsg);
            }
        });
    }

    @action
    saveInfoSpecial = (params = {}) => {
        let formParams = this.form.getFormParams();
        let newParams = {...params,...formParams, ids: this.ids };
        this.form.validateForm().then(f => {
            if (f.isValid) {
                if (this.type == 'specialUse') {
                    Apis.saveInfoSpecialUse(newParams).then(data => {
                        if (data.api_status == '1') {
                            if (!data.isRight) {
                                message.error(getLabel(2012, '对不起，您暂时没有权限！'));
                                return;
                            }
                            if (data.result == false) {
                                message.error(text.TIP8);
                                return ;
                            } else {
                                message.success(text.SAVESUCCESS);
                            }
                            if (this.by == 'list') {
                                window._table.reLoad();
                            } else {
                                window.close();
                                window.opener._table.reLoad();
                            }
                            this.setState({ visible: false });
                        } else {
                            message.error(data.api_errormsg);
                        }
                    });
                }
                if (this.type == 'specialReport') {
                    Apis.checkInfoPeriods(newParams).then(data => {
                        if (data.api_status == '1') {
                            if (!data.isRight) {
                                message.error(getLabel(2012, '对不起，您暂时没有权限！'));
                                return;
                            }
                            if (data.result == true) {
                                Apis.saveInfoSpecialReport(newParams).then(data => {
                                    if (data.api_status == '1') {
                                        if (data.result == false) {
                                            message.error(text.TIP9);
                                            return ;
                                        } else {
                                            message.success(text.SAVESUCCESS);
                                        }
                                        if (this.by == 'list') {
                                            window._table.reLoad();
                                        } else {
                                            window.close();
                                            window.opener._table.reLoad();
                                        }
                                        this.setState({ visible: false });
                                    } else {
                                        message.error(data.api_errormsg);
                                    }
                                });
                            } else {
                                message.error(text.TIP10);
                            }
                        }
                    });
                }
            } else {
                f.showErrors();
            }
        });
    }
}